/*
 * ElementSelectionPanel.java
 *
 * Created on 21.9.2009, 10:44:19
 */
package net.parostroj.timetable.gui.components;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import net.parostroj.timetable.gui.wrappers.Wrapper;
import net.parostroj.timetable.gui.wrappers.WrapperListModel;

/**
 * Panel for elements selection.
 *
 * @author jub
 */
public class ElementSelectionPanel<T> extends javax.swing.JPanel {

    private WrapperListModel<T> leftListModel;
    private WrapperListModel<T> rightListModel;

    /** Creates new form ElementSelectionPanel */
    public ElementSelectionPanel() {
        leftListModel = new WrapperListModel<T>();
        rightListModel = new WrapperListModel<T>();
        initComponents();
    }

    public void setListForSelection(List<Wrapper<T>> list) {
        leftListModel.setListOfWrappers(list);
        rightListModel.setListOfWrappers(new ArrayList<Wrapper<T>>());
    }

    public List<Wrapper<T>> getSelectedList() {
        return rightListModel.getListOfWrappers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JScrollPane scrollPane1 = new javax.swing.JScrollPane();
        leftList = new javax.swing.JList();
        moveRightButton = new javax.swing.JButton();
        moveLeftButton = new javax.swing.JButton();
        javax.swing.JScrollPane scrollPane2 = new javax.swing.JScrollPane();
        rightList = new javax.swing.JList();

        scrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        leftList.setModel(leftListModel);
        leftList.setPrototypeCellValue("mmmmmmmmmmmmmmmmmmmm");
        scrollPane1.setViewportView(leftList);

        moveRightButton.setText(">>");
        moveRightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveRightButtonActionPerformed(evt);
            }
        });

        moveLeftButton.setText("<<");
        moveLeftButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveLeftButtonActionPerformed(evt);
            }
        });

        scrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        rightList.setModel(rightListModel);
        rightList.setPrototypeCellValue("mmmmmmmmmmmmmmmmmmmm");
        scrollPane2.setViewportView(rightList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(scrollPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(moveRightButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(moveLeftButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollPane2))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(moveRightButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(moveLeftButton)
                .addContainerGap(80, Short.MAX_VALUE))
            .addComponent(scrollPane1)
            .addComponent(scrollPane2)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void moveLeftButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveLeftButtonActionPerformed
        // get selected elements and move them
        int[] values = rightList.getSelectedIndices();
        List<Wrapper<T>> toBeRemoved = new LinkedList<Wrapper<T>>();
        for (int ind : values) {
            Wrapper<T> wrapper = rightListModel.getIndex(ind);
            toBeRemoved.add(wrapper);
            leftListModel.addWrapper(wrapper);
        }
        for (Wrapper<T> w : toBeRemoved) {
            rightListModel.removeWrapper(w);
        }
    }//GEN-LAST:event_moveLeftButtonActionPerformed

    private void moveRightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveRightButtonActionPerformed
        // get selected elements and move them
        int[] values = leftList.getSelectedIndices();
        List<Wrapper<T>> toBeRemoved = new LinkedList<Wrapper<T>>();
        for (int ind : values) {
            Wrapper<T> wrapper = leftListModel.getIndex(ind);
            toBeRemoved.add(wrapper);
            rightListModel.addWrapper(wrapper);
        }
        for (Wrapper<T> w : toBeRemoved) {
            leftListModel.removeWrapper(w);
        }
    }//GEN-LAST:event_moveRightButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList leftList;
    private javax.swing.JButton moveLeftButton;
    private javax.swing.JButton moveRightButton;
    private javax.swing.JList rightList;
    // End of variables declaration//GEN-END:variables
}
